<template>
  <el-cascader
    ref="cascader"
    size="mini"
    :value="penpen"
    :options="depts"
    :props="props"
    separator=" | "
    @change="handleChange"
  />
</template>

<script>
import { getDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils'
export default {
  model: {
    prop: 'penpen',
    event: 'dapenpen'
  },
  props: {
    penpen: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      depts: [],
      props: {
        value: 'id',
        label: 'name'
      }
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async getDepartment() {
      const res = await getDepartment()
      this.depts = transListToTreeData(res.data, 0)
    },
    handleChange(value) {
      console.log(this.$refs.cascader)
      if (value.length > 0) {
        this.$emit('dapenpen', value[value.length - 1])
      } else {
        this.$emit('dapenpen', null)
      }
    }
  }
}
</script>

<style>

</style>
